﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;
using System.IO;


using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NCommon;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Dcjy04")]
    public class Dcjy04Controller : ApiController
    {   
        
        [HttpPost]
        [Route("PostDcjy04_inq")]

        //多记录查询
        public System.Data.DataSet PostDcjy04_inq([FromBody]  JObject obj)
        {
           
            //
            string ORDER_NO = obj["ORDER_NO"] == null ? " " : obj["ORDER_NO"].ToString();
            string PROD_CODE = obj["PROD_CODE"] == null ? " " : obj["PROD_CODE"].ToString();
            string PROD_CNAME = obj["PROD_CNAME"] == null ? " " : obj["PROD_CNAME"].ToString();


            

            string HARM_FLAG = obj["HARM_FLAG"] == null ? " " : obj["HARM_FLAG"].ToString();
            string SHIFT_NO = obj["SHIFT_NO"] == null ? " " : obj["SHIFT_NO"].ToString();
           
            string CHECK_SIGN = obj["CHECK_SIGN"] == null ? " " : obj["CHECK_SIGN"].ToString();
            string CHECK_TIME_START = obj["REC_CREATE_TIME"] == null ? " " : obj["REC_CREATE_TIME"].ToString();
            string CHECK_TIME_END = obj["REC_REVISE_TIME"] == null ? " " : obj["REC_REVISE_TIME"].ToString();

            string ROW_GUID = obj["ROW_GUID"] == null ? " " : obj["ROW_GUID"].ToString();

            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();

         
            

            
            /*
             * 分页程序需要重新写后台，暂时先不写后台
            string page_no = obj["PAGE_NO"] == null ? " " : obj["PAGE_NO"].ToString();
            string page_size = obj["PAGE_SIZE"] == null ? " " : obj["PAGE_SIZE"].ToString();
            string type_inq = obj["TYPE_INQ"] == null ? " " : obj["TYPE_INQ"].ToString();
            string userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();
            */

            //string sql_cols = " T1.ROW_GUID,T1.SAMPLE_LOT_NO,T1.SHIFT_NO,T1.SUPPLIER_NAME, T1.ORDER_NO,T1.VEHICLE_NO, T1.SAMPLE_NO_W, T1.SAMPLE_NO_H, T1.FACTORY_CODE, T1.SAMPLE_PLAN, T1.PROD_DIF, T1.PROD_CODE, T1.PROD_CNAME, T1.SEND_SAMPLE_TIME, T1.SAMPLE_NO, T1.CHECK_SIGN, T1.CHECK_PERSON_W, T1.CHECK_TIME_W, T1.CHECK_PERSON_H, T1.CHECK_TIME_H, T1.AUDIT_PERSON_NAME, T1.AUDIT_TIME,T1.WT,T1.SAMPLE_START_TIME,T1.SAMPLE_END_TIME,T1.SAMPLE_PLAN,T1.SAMPLE_PERSON,T1.WT,T1.TEST_TIMES,T1.REMARK,T1.SG_SIGN,T1.TEST_PERIOD,T1.RESULT_JUDGE, ";
         
            string sql_str1 = "  ";

            if (PROD_CODE.Trim() != "")
            {
                sql_str1 += "    WHERE    PROD_CODE  = '" + PROD_CODE + "' ";
            }
               
            else
            {
                if (PROD_CNAME.Trim() != "")
                    sql_str1 += "    WHERE    PROD_CNAME  = '" + PROD_CNAME + "' ";
            }


          
             




            if (ORDER_NO.Trim()!="")
                sql_str1 += "    and    ORDER_NO  like  '" +    ORDER_NO   +   "%' ";

             if (SHIFT_NO.Trim()!="")
                sql_str1 += "    and    SHIFT_NO  =  '" +    SHIFT_NO   +   "' ";


             //发布结果查询
            if (FORMNAME=="DCJY04PB")
            {
                sql_str1 += "  AND CHECK_SIGN IN ('6','7') ";    
            }
            else if (FORMNAME == "DCJY04")
            {
                if (CHECK_SIGN.Trim() != "")               
                sql_str1 += "    and    CHECK_SIGN  =  '" + CHECK_SIGN + "' ";
            }


            
             if (CHECK_TIME_START.Trim()!="")
                sql_str1 += "    and    SAMPLE_START_TIME  >=  '" +    CHECK_TIME_START   +   "' ";

             if (CHECK_TIME_END.Trim()!="")
                sql_str1 += "    and    SAMPLE_START_TIME  <=  '" +    CHECK_TIME_END   +   "' ";


             string sql_str2 = "";

            

             //主项查询
             if (ROW_GUID.Trim().Length < 5)
             {
                sql_str2 += " SELECT  *   FROM TDCJY01  " +  sql_str1;
             }
             else
             {
                 /*
                  * 最大值、最小值、平均值
                 sql_str2 += "  select ta.*,tb.max_act,tb.min_act, round(tb.avg_act,3) avg_act      ";
                 sql_str2 += "  from    ";
                 sql_str2 += "  ( SELECT elm_code,elm_name,elm_act  from   TDCJY02     ";
                 sql_str2 += "  where ROW_GUID = '" +   ROW_GUID    +  "'     ";
                 sql_str2 += "  ) ta,   ";
                 sql_str2 += "  (SELECT  ELM_CODE,max(elm_act) max_act ,min(elm_act) min_act ,avg(elm_act) avg_act from   TDCJY02     ";
                 sql_str2 += "  where ROW_GUID in (SELECT       ";
                 sql_str2 += "  ROW_GUID    ";
                 sql_str2 += "  FROM TDCJY01   " +  sql_str1 + ")";
                 sql_str2 += "  group by ELM_CODE)tb ";
                 sql_str2 += "  where ta.ELM_CODE = tb.ELM_CODE  order by ta.elm_name    ";
                 */

                // 仅有当前值
                 sql_str2 += "  SELECT elm_code,elm_name,elm_act  from   TDCJY02     ";
                 sql_str2 += "  where ROW_GUID = '" + ROW_GUID + "'     and elm_code != ' ' ";


             }
             EI.EIInfo outBlock = EF.Utility.ExecQuery(sql_str2);
           
       
         
         

            //图片数量
            //if (!(outBlock.Tables[0].Columns.Contains("FIELD_N1")))
            //{
            //    outBlock.Tables[0].Columns.Add("FIELD_N1");
            //}

           if (ROW_GUID.Trim().Length < 5)
           {
              
               EI.EIInfo outBlock_TEMP2 = EF.Utility.GetCodeClassValue("DC15", "DC39","DC77","QM03");
               DataTable dtTemp = null;

               
             
               
               for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
               {
                  
                   string check_sign = outBlock.Tables[0].Rows[i]["CHECK_SIGN"].ToString().Trim();
                   if (check_sign.Trim() != "")
                   {
                       if (outBlock_TEMP2.Tables["DC15"].Select("CODE  = '" + check_sign + "'").Count<DataRow>() == 1)
                       {
                           dtTemp = outBlock_TEMP2.Tables["DC15"].Select("CODE  = '" + check_sign + "'").CopyToDataTable<DataRow>();
                           outBlock.Tables[0].Rows[i]["CHECK_SIGN"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                       }
                   }
                   
                    	 
                   string shift_no = outBlock.Tables[0].Rows[i]["SHIFT_NO"].ToString().Trim();
                   if (shift_no.Trim() != "")
                   {
                       if (outBlock_TEMP2.Tables["DC39"].Select("CODE  = '" + shift_no + "'").Count<DataRow>() == 1)
                       {
                           dtTemp = outBlock_TEMP2.Tables["DC39"].Select("CODE  = '" + shift_no + "'").CopyToDataTable<DataRow>();
                           outBlock.Tables[0].Rows[i]["SHIFT_NO"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                       }
                   }
                     

                   string SAMPLE_PLAN = outBlock.Tables[0].Rows[i]["SAMPLE_PLAN"].ToString().Trim();
                   if (SAMPLE_PLAN.Trim() != "")
                   {
                       if (outBlock_TEMP2.Tables["DC77"].Select("CODE  = '" + SAMPLE_PLAN + "'").Count<DataRow>() == 1)
                       {
                           dtTemp = outBlock_TEMP2.Tables["DC77"].Select("CODE  = '" + SAMPLE_PLAN + "'").CopyToDataTable<DataRow>();
                           outBlock.Tables[0].Rows[i]["SAMPLE_PLAN"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                       }
                   }

                   string RESULT_JUDGE = outBlock.Tables[0].Rows[i]["RESULT_JUDGE"].ToString().Trim();
                   if (RESULT_JUDGE.Trim() != "")
                   {
                       if (outBlock_TEMP2.Tables["QM03"].Select("CODE  = '" + RESULT_JUDGE + "'").Count<DataRow>() == 1)
                       {
                           dtTemp = outBlock_TEMP2.Tables["QM03"].Select("CODE  = '" + RESULT_JUDGE + "'").CopyToDataTable<DataRow>();
                           outBlock.Tables[0].Rows[i]["RESULT_JUDGE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                       }
                   }

                   string hold_time = outBlock.Tables[0].Rows[i]["SAMPLE_START_TIME"].ToString();
                   if (hold_time.Length >= 12)
                       outBlock.Tables[0].Rows[i]["SAMPLE_START_TIME"] = hold_time.Substring(4, 2) + "-" + hold_time.Substring(6, 2) + " " + hold_time.Substring(8, 2) + ":" + hold_time.Substring(10, 2);

                   hold_time = outBlock.Tables[0].Rows[i]["SAMPLE_END_TIME"].ToString();
                   if (hold_time.Length >= 12)
                       outBlock.Tables[0].Rows[i]["SAMPLE_END_TIME"] = hold_time.Substring(4, 2) + "-" + hold_time.Substring(6, 2) + " " + hold_time.Substring(8, 2) + ":" + hold_time.Substring(10, 2);

                   hold_time = outBlock.Tables[0].Rows[i]["CHECK_TIME_W"].ToString();
                   if (hold_time.Length >= 12)
                       outBlock.Tables[0].Rows[i]["CHECK_TIME_W"] = hold_time.Substring(4, 2) + "-" + hold_time.Substring(6, 2) + " " + hold_time.Substring(8, 2) + ":" + hold_time.Substring(10, 2);

                   hold_time = outBlock.Tables[0].Rows[i]["CHECK_TIME_H"].ToString();
                   if (hold_time.Length >= 12)
                       outBlock.Tables[0].Rows[i]["CHECK_TIME_H"] = hold_time.Substring(4, 2) + "-" + hold_time.Substring(6, 2) + " " + hold_time.Substring(8, 2) + ":" + hold_time.Substring(10, 2);

                   hold_time = outBlock.Tables[0].Rows[i]["AUDIT_TIME"].ToString();
                   if (hold_time.Length >= 12)
                       outBlock.Tables[0].Rows[i]["AUDIT_TIME"] = hold_time.Substring(4, 2) + "-" + hold_time.Substring(6, 2) + " " + hold_time.Substring(8, 2) + ":" + hold_time.Substring(10, 2);
                   


                   



               }
           }

           outBlock.Tables[0].TableName = "Table0";





            outBlock.Tables.Add("COUNT");
            outBlock.Tables["COUNT"].Columns.Add("COUNT");
            outBlock.Tables["COUNT"].Rows.Add(1);







            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg + sql_str2);
            return outBlock;


        }

       

     

        [HttpPost]
        [Route("PostDcjy04_Load")]

        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】
        public System.Data.DataSet PostDcjy04_Load([FromBody]  JObject obj)
        {

            //小代码转换；
            EI.EIInfo outBlock = EF.Utility.GetCodeClassValue("DC15","DC39");

            DataRow dr1 = outBlock.Tables["DC15"].NewRow();
            dr1["CODE"] = " ";
            dr1["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["DC15"].Rows.InsertAt(dr1, 0);

            DataRow dr2 = outBlock.Tables["DC39"].NewRow();
            dr2["CODE"] = " ";
            dr2["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["DC39"].Rows.InsertAt(dr2, 0);


            

            //品名大类
            string sql_str1 = " select * from tep0002  tx where tx.code_class = 'DC81'  ";
                   sql_str1 += "    and code in (select    PROD_SORT_CODE FROM TSIDC01   ";
                   sql_str1 += "             where PROD_SORT_CODE!=' ')   ";
                   sql_str1 += "            order by CODE_DESC_1_CONTENT   ";  
            
            // EI.EIInfo outBlock2 = EF.Utility.GetCodeClassValue("DC81");
             EI.EIInfo outBlock2 = EF.Utility.ExecQuery(sql_str1);

            DataRow dr3 = outBlock2.Tables[0].NewRow();
            dr3["CODE"] = " ";
            dr3["CODE_DESC_1_CONTENT"] = " ";
            outBlock2.Tables[0].Rows.InsertAt(dr3, 0);

    
            outBlock2.Tables[0].TableName = "PROD_SORT_CODE";
             
            //品名明细；
            sql_str1 = "SELECT distinct  PROD_CODE ,PROD_CNAME,PROD_SORT_CODE FROM TSIDC01 ";
            sql_str1 += "  where PROD_SORT_CODE!=' '  ORDER BY PROD_CNAME  ";

            EI.EIInfo outBlock3 = EF.Utility.ExecQuery(sql_str1);
            outBlock3.Tables[0].TableName = "PROD_CODE";

            


          

            outBlock.Tables.Add(outBlock2.Tables[0].Copy());
            outBlock.Tables.Add(outBlock3.Tables[0].Copy());
         



            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }



 

    }
}